package com.meitutest.autoscript.V7;

import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;

import com.meitutest.autoscript.DeviceUtils;
import com.meitutest.autoscript.MyWatcher;
import com.meitutest.autoscript.utils.TestReport;

import java.util.Random;

/**
 * Created by CrystalChen on 2017/12/15.
 */

public class V7_GaoTongCameraUtils {
    final String pkg = "org.codeaurora.snapcam";
    final String act = "com.android.camera.CameraLauncher";
    static Random random = new Random();

    UiDevice uiDevice;
    DeviceUtils deviceUtils;

    public V7_GaoTongCameraUtils(UiDevice uiDevice) {
        this.uiDevice = uiDevice;
        this.deviceUtils = new DeviceUtils(uiDevice);
        MyWatcher watcher = new MyWatcher(uiDevice);
        uiDevice.registerWatcher("mywatcher", watcher);
    }

    public void openCamera() throws Exception {
        String cmd = "am start -n " + pkg + "/" + act;
        uiDevice.executeShellCommand(cmd);
        jumpGuider();
        TestReport.i("GaoTongCameraUtils_openCamera: 打开相机，" + pkg);
        if (!isCameraMode()) {
            uiDevice.findObject(new UiSelector().resourceId("org.codeaurora.snapcam:id/camera_switcher")).click();
            Thread.sleep(1000);
            uiDevice.findObject(new UiSelector().descriptionMatches("切换到拍照模式|Switch to photo")).click();
            Thread.sleep(1000);
        }
    }

    public void openVedio() throws Exception {
        String cmd = "am start -n " + pkg + "/" + act;
        uiDevice.executeShellCommand(cmd);
        jumpGuider();
//        TestReport.i("GaoTongCameraUtils_openCamera: 打开相机，" + pkg);
//        if (isCameraMode()) {
//            uiDevice.findObject(new UiSelector().resourceId("org.codeaurora.snapcam:id/video_button")).click();
//            Thread.sleep(1000);
//            uiDevice.findObject(new UiSelector().descriptionMatches("切换到视频模式|Switch to video")).click();
//            Thread.sleep(1000);
//        }
    }

    public void focusStressTest(int count) {
        try {
            openCamera();
            for (int i = 0; i < count; i++) {
                int x = random.nextInt(uiDevice.getDisplayWidth());
                int y = random.nextInt(uiDevice.getDisplayHeight() - 700) + 179;
                uiDevice.click(x, y);
                TestReport.i("DeviceUtils_randomClick: 第" + i + "次随机点击（" + x + "," + y + "）");
            }
            exitCamera();

        } catch (Exception e) {
            e.printStackTrace();
            TestReport.e("GaoTongCameraUtils_focusStressTest: " + e.getMessage());
        }
    }

    public void jumpGuider() throws Exception {
        while(uiDevice.hasObject(By.text("允许"))){
            uiDevice.findObject(By.text("允许")).click();
            Thread.sleep(1000);
        }
        Thread.sleep(1000);
        UiObject ok = uiDevice.findObject(new UiSelector().textMatches("确定|"));
        if (ok.exists()) {
            ok.click();
            TestReport.i("GaoTongCameraUtils_jumpGuider: 跳过引导页面");
        }

    }

    public void exitCamera() {
        while (deviceUtils.isInPkg(pkg)) {
            uiDevice.pressBack();
        }
        uiDevice.removeWatcher("mywathcer");
    }

    public void takePhotoStressTest(long period, long duration) {
        try {
            openCamera();
            long start = System.currentTimeMillis();
            while (System.currentTimeMillis() - start < duration) {
                TestReport.i("GaoTongCameraUtils_takePhotoStressTest: 1 " + period);
                Thread.sleep(period);
                TestReport.i("GaoTongCameraUtils_takePhotoStressTest: 2 " + period);
                UiObject shutter = uiDevice.findObject(new UiSelector().resourceId("org.codeaurora.snapcam:id/shutter_button"));
                shutter.click();
                TestReport.i("GaoTongCameraUtils_takePhotoStressTest: 拍照");
            }
            exitCamera();
        } catch (Exception e) {
            e.printStackTrace();
            TestReport.e("GaoTongCameraUtils_takePhotoStressTest: " + e.getMessage());
        }
    }

    public void vedioStressTest(long duration) {
        try {
            openVedio();
            uiDevice.findObject(new UiSelector().resourceId("org.codeaurora.snapcam:id/video_button")).click();
            TestReport.i("GaoTongCameraUtils_vedioStressTest: 开始录像");
            Thread.sleep(duration);
            uiDevice.findObject(new UiSelector().resourceId("org.codeaurora.snapcam:id/video_button")).click();
            TestReport.i("GaoTongCameraUtils_takePhotoStressTest: 录像结束");
            exitCamera();
        } catch (Exception e) {
            e.printStackTrace();
            TestReport.e("GaoTongCameraUtils_takePhotoStressTest: " + e.getMessage());
        }
    }

    public boolean isCameraMode() {
        UiObject auto = uiDevice.findObject(new UiSelector().resourceId("org.codeaurora.snapcam:id/scene_mode_switcher"));
        try {
            return auto.isClickable();
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
            return false;
        }

    }
}
